import { createRouter, createWebHistory } from 'vue-router'
import store from '../store'

// 布局组件
const Layout = () => import('../layout/Layout.vue')

// 公共页面
const Login = () => import('../views/Login.vue')
const Register = () => import('../views/Register.vue')

// 业务页面
const Dashboard = () => import('../views/Dashboard.vue')
const Clients = () => import('../views/client/ClientList.vue')
const ClientDetail = () => import('../views/client/ClientDetail.vue')
const Cargos = () => import('../views/cargo/CargoList.vue')
const CargoDetail = () => import('../views/cargo/CargoDetail.vue')
const PortAreas = () => import('../views/portarea/PortAreaList.vue')
const PortAreaDetail = () => import('../views/portarea/PortAreaDetail.vue')
const StorageRecords = () => import('../views/storage/StorageRecordList.vue')
const FeeRules = () => import('../views/fee/FeeRuleList.vue')
const StorageFees = () => import('../views/fee/StorageFeeList.vue')
const Invoices = () => import('../views/invoice/InvoiceList.vue')
const InvoiceDetail = () => import('../views/invoice/InvoiceDetail.vue')

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '首页', icon: 'el-icon-s-home' }
      },
      {
        path: 'clients',
        name: 'Clients',
        component: Clients,
        meta: { title: '客户管理', icon: 'el-icon-user' }
      },
      {
        path: 'clients/:id',
        name: 'ClientDetail',
        component: ClientDetail,
        meta: { title: '客户详情', activeMenu: '/clients' },
        hidden: true
      },
      {
        path: 'cargos',
        name: 'Cargos',
        component: Cargos,
        meta: { title: '货物管理', icon: 'el-icon-box' }
      },
      {
        path: 'cargos/:id',
        name: 'CargoDetail',
        component: CargoDetail,
        meta: { title: '货物详情', activeMenu: '/cargos' },
        hidden: true
      },
      {
        path: 'port-areas',
        name: 'PortAreas',
        component: PortAreas,
        meta: { title: '港口区域', icon: 'el-icon-location' }
      },
      {
        path: 'port-areas/:id',
        name: 'PortAreaDetail',
        component: PortAreaDetail,
        meta: { title: '区域详情', activeMenu: '/port-areas' },
        hidden: true
      },
      {
        path: 'storage-records',
        name: 'StorageRecords',
        component: StorageRecords,
        meta: { title: '仓储记录', icon: 'el-icon-document' }
      },
      {
        path: 'fee-rules',
        name: 'FeeRules',
        component: FeeRules,
        meta: { title: '费用规则', icon: 'el-icon-setting' }
      },
      {
        path: 'storage-fees',
        name: 'StorageFees',
        component: StorageFees,
        meta: { title: '仓储费用', icon: 'el-icon-money' }
      },
      {
        path: 'invoices',
        name: 'Invoices',
        component: Invoices,
        meta: { title: '发票管理', icon: 'el-icon-tickets' }
      },
      {
        path: 'invoices/:id',
        name: 'InvoiceDetail',
        component: InvoiceDetail,
        meta: { title: '发票详情', activeMenu: '/invoices' },
        hidden: true
      }
    ]
  },
  // 404页面重定向到首页
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = store.state.isAuthenticated
  
  if (to.meta.requiresAuth && !isAuthenticated) {
    next({ name: 'Login' })
  } else if (!to.meta.requiresAuth && isAuthenticated) {
    next({ name: 'Dashboard' })
  } else {
    next()
  }
})

export default router 